<?php
namespace Admin\Controller;

use Admin\Controller\GlobalController;

class StatisticController extends GlobalController
{
    //网站配置
    public function setting()
    {
        $sModel = M('site_setting');
        if (IS_POST) {
            //delete
            $deleteArr = I('post.delete');
            foreach ($deleteArr as $site_key) {
                if (in_array($site_key, array(
                    'weibo_access_token',
                    'update_versionNumber',
                    'update_versionLabel',
                    'update_url',
                    'update_description',
                    'update_type'
                ))) {
                    continue;
                }
                $sModel->where(array('site_key' => $site_key))->delete();
            }

            //edit
            $site_keys = I('post.site_key');
            $site_values = I('post.site_value');
            foreach ($site_keys as $id => $site_key) {
                $site_value = $site_values[$id];
                $sModel->where(array('site_key' => $site_key))->setField('site_value', $site_value);
            }

            //add
            $data['site_key'] = I('post.new_key', '', 'trim');
            $data['site_value'] = I('post.new_value', '', 'trim');
            if (!empty($data['site_key'])) {
                $sModel->add($data);
            }
        }
        $list = $sModel->order('id asc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    //崩溃日志列表
    public function crashlist()
    {
        $pageSize = 20;
        $where = array();
        $user_id = I('get.user_id', 0, 'intval');
        if (!empty($user_id)) {
            $where['uid'] = $user_id;
        }
        $this->assign('user_id', $user_id == 0 ? '' : $user_id);
        $crashModel = M('crash_log');
        $count = $crashModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $crashModel->where($where)->order('id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    public function downloadlist()
    {

        $pageSize = 10;
        $where = array();
        $ip = I('get.ip', '', 'trim');
        if (!empty($ip)) {
            $where['ip'] = $ip;
        }
        $this->assign('ip', $ip);
        $downloadModel = M('statistic_download');
        $count = $downloadModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $downloadModel->where($where)->order('id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());

        //按天统计
        $start_time = I('get.start_time', date('Y-m-d', time() - 86400 * 9), 'trim');
        $end_time = I('get.end_time', date('Y-m-d'), 'trim');
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time) + 86399;
        $ios_install = $downloadModel->field("FROM_UNIXTIME(download_time,'%Y%m%d') as the_date, count(*) as the_num")
            ->where("download_time > $start_time and download_time <= $end_time and 	app_type != 'android'")
            ->order('download_time desc')
            ->group('the_date')
            ->select();
        $ios_install = key2index($ios_install, 'the_date');
        $android_install = $downloadModel->field("FROM_UNIXTIME(download_time,'%Y%m%d') as the_date, count(*) as the_num")
            ->where("download_time > $start_time and download_time <= $end_time and 	app_type = 'android'")
            ->order('download_time desc')
            ->group('the_date')
            ->select();
        $android_install = key2index($android_install, 'the_date');
        //var_dump($ios_install, $android_install, $ios_start, $android_start);
        $statistic_list = array();
        for ($i = $end_time; $i >= $start_time; $i -= 86400) {
            $the_date = date('Ymd', $i);
            $statistic_list[$the_date]['ios_install'] = isset($ios_install[$the_date]) ? $ios_install[$the_date] : '';
            $statistic_list[$the_date]['android_install'] = isset($android_install[$the_date]) ? $android_install[$the_date] : '';
        }
        $this->assign('statistic_list', $statistic_list);
        $this->display();
    }

    //添加或者修改
    public function devicelist()
    {
        $pageSize = 10;
        $where = array();
        $unique_id = I('get.unique_id', '', 'trim');
        if (!empty($unique_id)) {
            $where['unique_id'] = $unique_id;
        }
        $this->assign('unique_id', $unique_id);
        $ip = I('get.ip', '', 'trim');
        if (!empty($ip)) {
            $where['ip'] = $ip;
        }
        $this->assign('ip', $ip);
        $deviceModel = M('statistic_device');
        $count = $deviceModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $deviceModel->where($where)->order('last_time desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());

        //按天统计
        $start_time = I('get.start_time', date('Y-m-d', time() - 86400 * 9), 'trim');
        $end_time = I('get.end_time', date('Y-m-d'), 'trim');
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time) + 86399;
        $ios_install = $deviceModel->field("FROM_UNIXTIME(first_time,'%Y%m%d') as the_date, count(*) as the_num")
            ->where("first_time > $start_time and first_time <= $end_time and os_type != 'android'")
            ->order('first_time desc')
            ->group('the_date')
            ->select();
        $ios_install = key2index($ios_install, 'the_date');
        $android_install = $deviceModel->field("FROM_UNIXTIME(first_time,'%Y%m%d') as the_date, count(*) as the_num")
            ->where("first_time > $start_time and first_time <= $end_time and os_type = 'android'")
            ->order('first_time desc')
            ->group('the_date')
            ->select();
        $android_install = key2index($android_install, 'the_date');
        //var_dump($ios_install, $android_install, $ios_start, $android_start);
        $statistic_list = array();
        for ($i = $end_time; $i >= $start_time; $i -= 86400) {
            $the_date = date('Ymd', $i);
            $statistic_list[$the_date]['ios_install'] = isset($ios_install[$the_date]) ? $ios_install[$the_date] : '';
            $statistic_list[$the_date]['android_install'] = isset($android_install[$the_date]) ? $android_install[$the_date] : '';
        }
        $this->assign('statistic_list', $statistic_list);
        $this->display();
    }
}